﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;

namespace AnesSystem.ViewModels
{
    public class AccountListCtrlViewModel : BaseViewModel<AccountListCtrlViewModel>
    {
        #region Variables

        private List<AccountModel> _disAccountList = new List<AccountModel>();

        private List<AccountModel> _accountList = new List<AccountModel>();

        private bool _isOpen = false;

        private string _selectedDoctor;

        private bool _canSearch = true;

        #endregion

        #region Constructor

        public AccountListCtrlViewModel()
        {
            InitData();
        }

        #endregion

        #region Properties

        public List<AccountModel> AccountList
        {
            get
            {
                return _accountList;
            }

            set
            {
                _accountList = value;
                RaisePropertyChanged("AccountList");
            }
        }

        public bool IsOpen
        {
            get
            {
                return _isOpen;
            }

            set
            {
                _isOpen = value;
                RaisePropertyChanged("IsOpen");
            }
        }

        public string SelectedDoctor
        {
            get
            {
                return _selectedDoctor;
            }

            set
            {
                _selectedDoctor = value;
                RaisePropertyChanged("SelectedDoctor");
            }
        }

        public List<AccountModel> DisAccountList
        {
            get
            {
                return _disAccountList;
            }

            set
            {
                _disAccountList = value;
                RaisePropertyChanged("DisAccountList");
            }
        }

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods

        public void Serch(string input)
        {
            DisAccountList = _accountList.Where(O => O != null && !string.IsNullOrEmpty(O.TrueName) && O.TrueName.IsContain(input)).ToList();
        }

        #endregion

        #region EventHandlers

        #endregion


        #region Command

        #region ChangeDoctorCommand

        private void ChangeDoctorExecute(AccountModel param)
        {
            _canSearch = false;
            SelectedDoctor = param.TrueName;
            _canSearch = true;
        }

        private bool CanChangeDoctorExecute(object obj)
        {
            return true;
        }

        private RelayCommand<AccountModel> _changeDoctorCommand;

        public RelayCommand<AccountModel> ChangeDoctorCommand
        {
            get
            {
                if (_changeDoctorCommand == null)
                {
                    _changeDoctorCommand = new RelayCommand<AccountModel>(ChangeDoctorExecute, CanChangeDoctorExecute);
                }
                return _changeDoctorCommand;
            }
        }

        #endregion

        #region OpenPopupCommand

        private void OpenPopupExecute(object obj)
        {
            IsOpen = !_isOpen;
        }

        private bool CanOpenPopupExecute(object param)
        {
            return true;
        }

        private RelayCommand _openPopupCommand;

        public RelayCommand OpenPopupCommand
        {
            get
            {
                if (_openPopupCommand == null)
                {
                    _openPopupCommand = new RelayCommand(OpenPopupExecute, CanOpenPopupExecute);
                }
                return _openPopupCommand;
            }
        }

        private RelayCommand<string> _searchCommand;

        public RelayCommand<string> SearchCommand
        {
            get
            {
                if (_searchCommand == null)
                {
                    _searchCommand = new RelayCommand<string>(SerchExecute, CanSearchExecute);
                }
                return _searchCommand;
            }
        }

        private bool CanSearchExecute(object obj)
        {
            return _canSearch;
        }

        private void SerchExecute(string obj)
        {
            try
            {
                AccountList = _accountList.Where(O => O.TrueName.IsContain(obj.ToString())).ToList();
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType().Name, ex);
            }

        }
        private void InitData()
        {
            _accountList.Clear();
            _disAccountList.Clear();
            IEnumerable<AccountModel> list = AccountService.Instance.FindAll().ToList(); 
            foreach (var item in list)
            {
                _accountList.Add(item);
                item.PropertyChanged += Item_PropertyChanged;
            }

            foreach (var item in _accountList)
            {
                _disAccountList.Add(item);
            }
            //ConfigAccountCtrlViewModel.Instance.Refresh();
        }
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (sender is AccountModel)
            {
                AccountService.Instance.Update(sender as AccountModel);
            }
        }
        public override void Refresh()
        {
            InitData();
        }
        #endregion
        #endregion

    }
}
